package huawei;

import java.util.*;
import java.util.stream.Collectors;

/**
 * 计算机网络信号
 */
public class Q111 {

    static int[][] direction = {{0,1},{0,-1},{1,0},{-1,0}};
    
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        
        while (scanner.hasNext()) {
            String string1 = scanner.nextLine();
            String string2 = scanner.nextLine();
            String string3 = scanner.nextLine();

            List<Integer> collect = Arrays.stream(string1.split(" ")).map(String::trim).map(Integer::parseInt).collect(Collectors.toList());

            int[][] ints = new int[collect.get(0)][collect.get(1)];
            List<Integer> collect1 = Arrays.stream(string2.split(" ")).map(String::trim).map(Integer::parseInt).collect(Collectors.toList());
            int k = 0;
            for (int i = 0; i < ints.length; i++) {
                for (int j = 0; j < ints[i].length; j++) {
                    ints[i][j] = collect1.get(k ++);
                }
            }

            Queue<int[]> queue = new LinkedList<>();

            // 找出所有的发光原
            for (int i = 0; i < ints.length; i++) {
                for (int j = 0; j < ints[i].length; j++) {
                   if (ints[i][j] > 0) {
                       queue.offer(new int[]{i, j});
                   }
                }
            }

            while (!queue.isEmpty()) {
                int[] poll = queue.poll();
                int x = poll[0];
                int y = poll[1];

                int v = ints[x][y];
                for (int[] direction : direction) {
                    int newX = x + direction[0];
                    int newY = y + direction[1];

                    if (newX >= 0 && newY >=0 && newX < ints.length && newY < ints[0].length
                            && ints[newX][newY] != -1
                            && ints[newX][newY] < v - 1) {
                        ints[newX][newY] = v - 1;
                        queue.offer(new int[]{newX, newY});
                    }
                }
            }

            List<Integer> collect2 = Arrays.stream(string3.split(" ")).map(String::trim).map(Integer::parseInt).collect(Collectors.toList());
            int i = collect2.get(0);
            int j = collect2.get(1);

            System.out.println(ints[i][j]);
        }

    }


}
